#!/usr/bin/python3"
import re
print("练习2：")
print("1.进入/lianxi目录，复制/etc/passwd到当前目录下，然后对passwd进行操作")

print("2、查找出当前passwd文件中以ftp或者mail开头的行，在屏幕上输出。")
f = open('passwd')
for i in f:
    if re.match('^ftp|^mail',i):
        print(i,end="")
f.close()
print("3、查找出当前passwd文件中首行不是以r、m、f开头的行，在屏幕上输出。")
f = open('passwd')
for s in f:
    if re.findall('^[^rmf]',s):
        print(s,end="")
f.close()
print("4、查找出当前passwd文件中以bash结尾的行。")
with open('passwd') as f:
    for j in f:
        if re.findall('bash$',j):
            print(j,end="")
print("5、查找出/etc/login.defs文件中的有效行（不显示空行和注释行）。")
with open('/etc/login.defs') as f:
    for k in f:
        if re.findall("^[^#\s]",k):
            print(k,end="")
print("6、查找出/var/log/messages文档中有16个字母的单词？")
m = re.compile(r'[a-zA-Z]{15}')
with open('/var/log/messages') as f:
    for d in f:
        if m.findall(d):
            print(m.findall(d))
        
print("7、查找出来/etc/passwd文件里用户名包含liu同时使用bash的用户")
with open('passwd') as f:
    for t in f:
        if re.findall('.*kong.*:x:.*bash$',t):
            print(t,end="")
print("8、查找/etc/ssh/sshd_config 里的有效行")
with open('/etc/ssh/sshd_config') as f:
    for y in f:
        if re.findall('^[^#|\s]',y):
            print(y,end="")
print("9、查找出/etc/ssh/sshd_config 文件里的包含连续2个数字的行")
with open("/etc/ssh/sshd_config") as f:
    for yn in f:
        if re.findall('\d{2}',yn):
            print(yn,end="")
print("10、查找出包含特殊字符的行")
with open("/etc/ssh/sshd_config") as f: 
    for ts in f:
        if re.findall('[^\w]',ts):
            print(ts,end="")
print("11、查找出不包含数字的行")
with open("/etc/ssh/sshd_config") as f:
    for nn in f:
        if re.findall('[^\d]',nn):
            print(nn,end="")
print("12、查找出/var/log/secure里的ip地址出来")
with open('/var/log/secure') as f:
    for ip in f:
        if re.findall('(?:\d{1,3}\.){3}\d+',ip):
            print(re.findall('(?:\d{1,3}\.){3}\d+',ip))
